package online.hxstart.code.concurrent.chapter12;

import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;
import online.hxstart.code.concurrent.chapter10.TempData;

/**
 * @author huxing
 * @date 2019/7/26 周五 11:38
 * @describe
 */
public class FileCount {

  public static void main(String[] args) throws IOException {
    Set<TempData> phone = new HashSet<>();
    File file = new File("D:\\debug\\debug-8.6.log");
    try (FileInputStream fileSystem = new FileInputStream(file);
        BufferedReader reader = new BufferedReader(new InputStreamReader(fileSystem,
            StandardCharsets.UTF_8));) {
      String line;
      while ((line = reader.readLine()) != null) {
        if (line.contains("ARGS:[ActivityMsg(id=null, userId=null,")) {
          // 打印当前行字符串
          phone.add(getMap(line));
        }
      }
    }

    System.out.println(phone.size());
    System.out.println(JSON.toJSONString(phone));
  }

  private static TempData getMap(String string) {
    String
        temp = string.substring(string.lastIndexOf("id=null, userId=null"));
    String[] a = temp.split(", ");
    TempData tempData = new TempData();
    for (String s : a) {
      String[] aa = s.split("=");
      if ("userName".equals(aa[0])) {
        tempData.setUserName(aa[1]);
      }
      if ("userPhone".equals(aa[0])) {
        tempData.setUserPhone(aa[1]);
      }
      if ("dataSource".equals(aa[0])) {
        tempData.setDataSource(aa[1]);
      }
    }
    return tempData;
  }
}
